package com.neusoft.crm.mapper;

import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.po.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 销售机会数据层
 *
 * @author msprc
 */
@Mapper
public interface SaleChanceMapper {

    /**
     * 根据状态查找销售机会
     *
     * @param saleChance 销售机会
     * @return 销售机会列表
     */
    List<SaleChance> selectSaleChanceByStatus(SaleChance saleChance);

    /**
     * 添加销售机会
     *
     * @param saleChance 销售机会
     * @return 受影响的行数
     */
    @Insert("insert into SaleChance values(null,#{chanceSource},#{chanceCustName},#{chanceTitle},#{chanceRate},#{chanceLinkman},#{chanceTel},#{chanceDesc},#{chanceCreateId},#{chanceCreateDate},null,null,#{chanceStatus})")
    int insertSaleChance(SaleChance saleChance);

    /**
     * 修改销售机会
     *
     * @param saleChance 销售机会
     * @return 受影响的行数
     */
    @Update("update SaleChance set chanceSource=#{chanceSource},chanceCustName=#{chanceCustName},chanceTitle=#{chanceTitle},chanceRate=#{chanceRate},chanceLinkman=#{chanceLinkman},chanceTel=#{chanceTel},chanceDesc=#{chanceDesc},chanceCreateId=#{chanceCreateId},chanceCreateDate=#{chanceCreateDate},chanceDueId=#{chanceDueId},chanceDueDate=#{chanceDueDate},chanceStatus=#{chanceStatus} where chanceId=#{chanceId}")
    int updateSaleChance(SaleChance saleChance);

    /**
     * 删除销售机会（物理删除）
     *
     * @param saleChance 销售机会
     * @return 受影响的行数
     */
    @Delete("delete from SaleChance where chanceId=#{chanceId}")
    int deleteSaleChance(SaleChance saleChance);

    /**
     * 指派销售机会
     *
     * @param saleChance 销售机会
     * @return 受影响的行数
     */
    @Update("update SaleChance set chanceDueId=#{chanceDueId},chanceDueDate=#{chanceDueDate},chanceStatus=#{chanceStatus} where chanceId=#{chanceId}")
    int assignSalesChance(SaleChance saleChance);

    /**
     * 查找已经指派给我的销售机会
     *
     * @param saleChance 目标用户的ID
     * @return 销售机会列表
     */
    List<SaleChance> selectAssignedSalesChance(SaleChance saleChance);

    /**
     * 通过模糊查询查找销售机会
     *
     * @param saleChance 销售机会
     * @return 机会列表
     */
    List<SaleChance> selectSaleChanceByTag(SaleChance saleChance);

    /**
     * 通过id精确查询查找销售机会
     *
     * @param saleChance 销售机会
     * @return 机会列表
     */
    SaleChance selectSalesChanceById(SaleChance saleChance);

    /**
     * 通过销售机会状态，state=3，返回系统用户
     *
     * @param saleChance 销售机会
     * @return 系统用户对象
     */
    @Select("select * from sysuser as sy where sy.userRoleId=3 and sy.delMark=1")
    List<SysUser> selectSysUserNameBySaleChanceState(SaleChance saleChance);
}
